package com.tongtech.proxy.core.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class StringEncryptUtil {
    private static final String[] ALGORITHMS = new String[]{
            "MD5", "SHA-1", "SHA-256", "SHA-384", "SHA-512"
    };

    public static String encrypt(String str, String algorithm) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        byte[] bytes = md.digest(str.getBytes());
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static String encrypt(String str) throws NoSuchAlgorithmException {
        return encrypt(str, ALGORITHMS[0]);
    }

    public static String encryptAll(String str) throws NoSuchAlgorithmException {
        StringBuilder sb = new StringBuilder();
        for (String algorithm : ALGORITHMS) {
            sb.append(algorithm).append(":").append(encrypt(str, algorithm)).append("\n");
        }
        return sb.toString();
    }
}
